<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class LanguageBean {

    public function LanguageBean() {
    }
    
    /*Checks, if $_POST['language_name'] and $POST_['language_key']
     * are empty and returns an error message for this case.
     */ 
    public function validate_language() {
    	$msg = "";
    	if($_POST['language_name'] == "") {
    		$msg = _("Language name can not be empty");
    	}
    	if($_POST['language_key'] == "") {
    		if($msg != "") $msg .= "<br />";
    			$msg .= _("Language key can not be empty");
    	}
    	return $msg;
    }
    
    //Stores the language_name and the language_key of the array 
    //$language to the database and returns the new id generated from 
    //the database.
    public function save_language($db, $language) {
    	$res = $db->query(sprintf(
					"INSERT INTO %s(lang_name,lang_key) " .
					"VALUES('%s','%s')",
    				TBL_MULTI_LANGUAGE, 
    				mysql_real_escape_string($language['language_name']),
					mysql_real_escape_string($language['language_key'])));
    	
    	if($res == true)
    		return $db->insert_id;
    	else
    		return -1;
    }
    
    
    /*
     * Get all data from ntp_language table
     * each item is an object same as database structure. 
     * The object structure of $res, is a result of using ezSql. 
     */
    public function get_all_languages($db) {
    	$strSQL = sprintf("select * from %s" .
    						" where %1\$s.active = '1'", TBL_LANGUAGE_LIST);
    	$res = $db->get_results($strSQL);
    	return $res;
    }
    
    /* Deletes language with given id in database. 
     * Returns result of the query
     */
    public function delete_language($db, $id) {
    	//Delete Partner
    	$strSQL = sprintf("DELETE FROM %1\$s" .
				" where %1\$s.lang_id = '$id'", 
				TBL_LANGUAGE);
		
    	$res = $db->query($strSQL);
  	
    	return $res;
    }
    
    //get_language_by_id, (Result is an object, because of using ezSql)
    public function get_language_by_id($db, $id) {
    	$strSQL = sprintf("SELECT * FROM %s WHERE lang_id='$id'", TBL_MULTI_LANGUAGE);
    	return $db->get_row($strSQL);
    }
    
    //Updates a language entry in the database
    public function update_language($db, $language) {
    	$res = $db->query(sprintf(
					"UPDATE %s SET " .
					"lang_name	= '%s', " .
					"lang_key	= '%s' " .
					"WHERE lang_id = '%s'",
    				TBL_MULTI_LANGUAGE,
    				mysql_real_escape_string($language[language_name]),
    				mysql_real_escape_string($language[language_key]),
    				mysql_real_escape_string($language[language_id])));
    	
    	return $res;
    }
}
?>
